package com.caregiver.watch.business.care.service.strategy.impl;

import com.alibaba.fastjson2.JSON;
import com.caregiver.watch.business.care.dao.HealthAlertConfigDao;
import com.caregiver.watch.business.care.enums.HealthAlertTypeEnum;
import com.caregiver.watch.business.care.model.bo.AlertMessageBo;
import com.caregiver.watch.business.care.model.bo.CardiacHealthWarningConfig;
import com.caregiver.watch.business.care.model.bo.FallDetectionWarningConfig;
import com.caregiver.watch.business.care.model.po.HealthAlertConfig;
import com.caregiver.watch.business.care.service.UserAlertRecordService;
import com.caregiver.watch.business.care.service.strategy.AbstractHealthWarningStrategy;
import com.caregiver.watch.business.data.protobuf.AlarmInfo;
import com.caregiver.watch.business.user.model.po.UserCareRecipient;
import com.caregiver.watch.business.user.service.UserDeviceService;
import com.caregiver.watch.common.utils.time.DateUtils;
import com.caregiver.watch.thirdparty.service.JPushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 心脏健康预警策略
 * @Author 疆戟
 * @Date 2025/11/2 17:39
 * @Version 1.0
 */
@Component
@Slf4j
public class CardiacHealthWarningStrategy extends AbstractHealthWarningStrategy<List<AlarmInfo.ThrombusAlarmV3>> {

    public CardiacHealthWarningStrategy(UserDeviceService userDeviceService, HealthAlertConfigDao healthAlertConfigDao, UserAlertRecordService userAlertRecordService, JPushService jPushService) {
        super(userDeviceService, healthAlertConfigDao, userAlertRecordService, jPushService);
    }

    @Override
    public void handle(Long userId, String deviceUuId, List<AlarmInfo.ThrombusAlarmV3> data) {
        log.info("处理心脏健康预警数据:{}", data);
        if (CollectionUtils.isEmpty(data)) {
            log.warn("未查询到心脏健康预警数据");
            return;
        }
        HealthAlertConfig config = getConfig();
        if (Objects.isNull(config) || BooleanUtils.isNotTrue(config.getEnabled())) {
            log.warn("未查询到心脏健康预警配置");
            return;
        }
        String alertConfig = config.getAlertConfig();
        log.info("心脏健康预警配置：{}", alertConfig);
        if (StringUtils.isBlank(alertConfig)) {
            log.warn("未配置心脏健康预警配置");
            return;
        }
        // 获取被关照人
        UserCareRecipient userCareRecipient = queryContactDeviceDetail(userId, deviceUuId);
        if (Objects.isNull(userCareRecipient)) {
            log.warn("未查询到被关照人");
            return;
        }
        AlarmInfo.ThrombusAlarmV3 thrombusAlarm = data.get(ZERO);
        List<CardiacHealthWarningConfig> cardiacHealthWarningConfigs = JSON.parseArray(alertConfig, CardiacHealthWarningConfig.class);
        // 获取告警时间
        LocalDateTime alarmTime = DateUtils.convert2LocalDateTime(thrombusAlarm.getTimeStamp().getDateTime().getSeconds());
        CardiacHealthWarningConfig warningConfig = cardiacHealthWarningConfigs.get(ZERO);
        AlertMessageBo alertMessage = AlertMessageBo.build(warningConfig.getWarningTitle(), StringUtils.EMPTY, StringUtils.EMPTY, warningConfig.getHandleSuggestions());
        // 发送告警
        Boolean sendPush = sendPush(userId, userCareRecipient.getRecipientId(), config.getAlertType(), alarmTime, alertMessage);
        if (BooleanUtils.isTrue(sendPush)) {
            // 记录告警
            saveAlertRecord(userId, userCareRecipient.getRecipientId(), config.getAlertType(), alarmTime, JSON.toJSONString(alertMessage));
        }
    }

    @Override
    public HealthAlertTypeEnum getAlertType() {
        return HealthAlertTypeEnum.HEART_HEALTH_WARNING;
    }
}
